﻿//
// Les chaines de texte des exceptions ne sont pas localisées car elles sont à destination des administrateurs.
//
using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using FranceTriple.Dal;

namespace TeamFranceTriple
{
    public partial class activation : BasePage
    {
        private static string prefixTitle = "Team France Triple" + " - ";
        private static string ActivationSuccessLocalized = "ActivationSuccess".ToLocalizedString();

        Guid userKey;
        MembershipUser userToActivate = null;
        ContentPlaceHolder bodyContent;
        ActivationSuccess activationSuccess;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.RouteData.Values.Count == 0)
                throw new HttpException(403, "(Routing Error) Invalid user account activation attempt with no key.");

            userKey = Guid.Parse(Page.RouteData.Values["activationKey"].ToString());
            if (userKey != null)
                userToActivate = Membership.GetUser(userKey, false);
            else
                throw new HttpException(403, "(ActivationKey is null) Invalid user account activation attempt for key : " + userKey.ToString().TextToHtml());

            if (userToActivate == null)
                throw new HttpException(403, "(Unknown user) Invalid user account activation attempt for key : " + userKey.ToString().TextToHtml());

            // Test pour éviter le scénario suivant : un utilisateur est désactivé par un administrateur et se réactive tout seul en réutilisant l'email.
            // Du coup le lien d'activation de l'email ne marchera que la première fois.
            if ((userToActivate.CreationDate == userToActivate.LastLoginDate) && (userToActivate.CreationDate == userToActivate.LastActivityDate))
            {
                userToActivate.IsApproved = true;
                Membership.UpdateUser(userToActivate);
            }
            else
                throw new HttpException(403, "(Not first time activation) : Invalid user account activation attempt for key : " + userKey.ToString().TextToHtml());

            activationSuccess = LoadControl("~/Account/ActivationSuccess.ascx") as ActivationSuccess;
            bodyContent = ThisMaster.FindControl("MainContent") as ContentPlaceHolder;
            bodyContent.Controls.Add(activationSuccess);
            Page.Title = prefixTitle + ActivationSuccessLocalized;
        }
    }
}